package com.androwit.engine.cassandra;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import me.prettyprint.cassandra.serializers.BytesArraySerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.ddl.ComparatorType;

import com.androwit.engine.mapping.MappingField;
import com.androwit.engine.mapping.converter.str.UuidConverter;

@SuppressWarnings("unchecked")
public final class MappingFieldsDefinerMock extends
		HectorMappingFieldsDefiner<String, String, String> {
	HectorMappingField<String, String, String>[] fields = new HectorMappingField[] {

			new HectorMappingField<UUID, String, String>(
					HectorPersisterTest.C_UUID, new UuidConverter(), "uuid",
					null, StringSerializer.get(), ComparatorType.UTF8TYPE, true),
			new HectorMappingField<String, String, byte[]>(
					HectorPersisterTest.C_BYTES, null, "bytes", null,
					BytesArraySerializer.get(), ComparatorType.BYTESTYPE, false),
			new HectorMappingField<String, String, String>(
					HectorPersisterTest.C_STRING, null, "string", null,
					StringSerializer.get(), ComparatorType.UTF8TYPE, false),
			new HectorMultiColumnMappingField<String, String, List<SimplePojo>, SimplePojo, byte[], SimplePojo>(
					HectorPersisterTest.C_MULTI_STRING, null, "list",
					StringSerializer.get(), BytesArraySerializer.get()) {

				@Override
				protected String toKey(SimplePojo dynamicColumn) {
					return key + "_" + dynamicColumn.uuid;
				}

				@Override
				protected byte[] toValue(SimplePojo dynamicColumn) {
					return dynamicColumn.string.getBytes();
				}

				@Override
				protected boolean isMultiColumn(String key) {
					return key.startsWith(this.key);
				}

				@Override
				protected SimplePojo fromValue(String key, byte[] dynColumnValue) {
					String uuid = key.substring(this.key.length() + 1,
							key.length());
					return new SimplePojo(UUID.fromString(uuid),
							dynColumnValue, new String(dynColumnValue));
				}
			}

	};

	@Override
	public SimplePojo createInstance(Map<String, String> values) {
		return new SimplePojo();
	}

	@Override
	public String getKey(Object input) {
		return ((SimplePojo) input).uuid.toString();
	}

	@Override
	protected String getColumnFamily() {
		return HectorPersisterTest.COLUMN_FAMILY;
	}

	@Override
	protected MappingField<String, String, String>[] getLocalMappingFields(
			Map<String, String> values) {
		return fields;
	}

	@Override
	protected MappingField<String, String, String>[] getLocalMappingFields(
			Object topersist) {
		return fields;
	}
}